package com.masterclient.app.modules.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.baselibrary.bean.order.InvoiceBean;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.bridge.BridgeActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/7/25 15:34
 * 功能说明：
 */

public class InvoiceActivity extends BaseActivity {


    public static final int RESULT_CODE = 102;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.tv_tag1)
    TextView tvTag1;
    @BindView(R.id.tv_type_name)
    TextView tvTypeName;
    @BindView(R.id.iv_type_arrow)
    ImageView ivTypeArrow;
    @BindView(R.id.rl_invoice_type)
    RelativeLayout rlInvoiceType;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_tag2)
    TextView tvTag2;
    @BindView(R.id.tv_paper_invoice_type)
    TextView tvPaperInvoiceType;
    @BindView(R.id.iv_zengzhi_arrow)
    ImageView ivZengzhiArrow;
    @BindView(R.id.ll_special_invoice)
    LinearLayout llSpecialInvoice;
    @BindView(R.id.et_ele_company_name)
    EditText etEleCompanyName;
    @BindView(R.id.et_ele_company_id)
    EditText etEleCompanyId;
    @BindView(R.id.et_ele_company_email)
    EditText etEleCompanyEmail;
    @BindView(R.id.ll_ele_company)
    LinearLayout llEleCompany;
    @BindView(R.id.et_ele_person_name)
    EditText etElePersonName;
    @BindView(R.id.et_ele_person_email)
    EditText etElePersonEmail;
    @BindView(R.id.ll_ele_person)
    LinearLayout llElePerson;
    @BindView(R.id.et_paper_person_name)
    EditText etPaperPersonName;
    @BindView(R.id.ll_paper_person)
    LinearLayout llPaperPerson;
    @BindView(R.id.et_paper_company_name)
    EditText etPaperCompanyName;
    @BindView(R.id.et_paper_company_id)
    EditText etPaperCompanyId;
    @BindView(R.id.et_paper_company_addr)
    EditText etPaperCompanyAddr;
    @BindView(R.id.et_paper_company_phone)
    EditText etPaperCompanyPhone;
    @BindView(R.id.et_paper_company_bank)
    EditText etPaperCompanyBank;
    @BindView(R.id.et_paper_company_bankid)
    EditText etPaperCompanyBankid;
    @BindView(R.id.ll_paper_company)
    LinearLayout llPaperCompany;
    @BindView(R.id.et_paper_company_name_spe)
    EditText etPaperCompanyNameSpe;
    @BindView(R.id.et_paper_company_id_spe)
    EditText etPaperCompanyIdSpe;
    @BindView(R.id.et_paper_company_addr_spe)
    EditText etPaperCompanyAddrSpe;
    @BindView(R.id.et_paper_company_phone_spe)
    EditText etPaperCompanyPhoneSpe;
    @BindView(R.id.et_paper_company_bank_spe)
    EditText etPaperCompanyBankSpe;
    @BindView(R.id.et_paper_company_bankid_spe)
    EditText etPaperCompanyBankidSpe;
    @BindView(R.id.ll_paper_company_special)
    LinearLayout llPaperCompanySpecial;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.tv_label)
    TextView tvLabel;

    private String fapiao_type = "2"; //0不开，1电子，2纸质
    private String name_type = "1";//0不开，1个人，2单位
    private String vat_type = "2";//1专用2普通

    String shop_id;
    String position;
    List<String> invoice_type;
    String url;
    private String label;

    private String invoice_type_1//
            , invoice_type_2;

    InvoiceBean mInvoiceBean;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invoice);
        ButterKnife.bind(this);
        shop_id = getBundleString("shop_id");
        position = getBundleString("position");
        label = getBundleString("label");
        tvLabel.setText(label);
        invoice_type = (List<String>) getIntent().getSerializableExtra("invoice_type");
        if(getIntent().getExtras()!=null){
            mInvoiceBean = (InvoiceBean) getIntent().getExtras().getSerializable("invoice");
        }
        url = getBundleString("url");

        if (invoice_type.size() >= 1) {
            invoice_type_1 = invoice_type.get(0);
        }
        if (invoice_type.size() >= 2) {
            invoice_type_2 = invoice_type.get(1);
        }
        if ("1".equals(invoice_type_1)) {
            tvTypeName.setText("电子发票");
            rlInvoiceType.setClickable(false);
            ivTypeArrow.setVisibility(View.GONE);
            fapiao_type = "1";
            changeInvoiceTable("1", "1");
        } else if ("2".equals(invoice_type_1)) {
            tvTypeName.setText("纸质发票");
            rlInvoiceType.setClickable(false);
            ivTypeArrow.setVisibility(View.GONE);
            fapiao_type = "2";
            changeInvoiceTable("2", "1");
        }

        if(mInvoiceBean!=null){
            name_type = mInvoiceBean.getName_type();
            if("1".equals(fapiao_type)){
                tvTypeName.setText("电子发票");
            }else {
                tvTypeName.setText("纸质发票");
            }

            fapiao_type = mInvoiceBean.getType();

            if ("1".equals(name_type)) {
                tvPerson.setTextColor(0xffcc363c);
                tvPerson.setBackgroundResource(R.drawable.shape_line_212121);
                tvCompany.setTextColor(0xff333333);
                tvCompany.setBackgroundResource(R.drawable.fapiao_selectable);
            }else if("2".equals(fapiao_type)){
                tvCompany.setTextColor(0xffcc363c);
                tvCompany.setBackgroundResource(R.drawable.shape_line_212121);
                tvPerson.setTextColor(0xff333333);
                tvPerson.setBackgroundResource(R.drawable.fapiao_selectable);
            }

            vat_type = mInvoiceBean.getVat_type();
        }
        changeInvoiceTable(name_type,fapiao_type);

    }

    @OnClick({R.id.rl_back, R.id.rl_invoice_type, R.id.tv_person, R.id.tv_company, R.id.ll_special_invoice,R.id.tv_know})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_know:
                startActivitry(BridgeActivity.class,new String[][]{{"url",url}});
                break;
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_invoice_type:
                InvoiceTypeDialog invoiceTypeDialog = new InvoiceTypeDialog(mContext, fapiao_type);
                invoiceTypeDialog.show();
                invoiceTypeDialog.setOnConfirmListener(new InvoiceTypeDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm(String type) {
                        fapiao_type = type;
                        if ("2".equals(type)) {
                            tvTypeName.setText("纸质发票");
                        } else if ("1".equals(type)) {
                            tvTypeName.setText("电子发票");
                        }
                        changeInvoiceTable(name_type, fapiao_type);
                    }
                });
                break;
            case R.id.ll_special_invoice:
                PaperInvoiceTypeDialog dialog = new PaperInvoiceTypeDialog(mContext, vat_type);
                dialog.show();
                dialog.setOnConfirmListener(new PaperInvoiceTypeDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm(String type) {
                        vat_type = type;
                        if ("1".equals(vat_type)) {
                            tvPaperInvoiceType.setText("增值税专用发票");
                        } else if ("2".equals(vat_type)) {
                            tvPaperInvoiceType.setText("增值税普通发票");
                        }
                        changeInvoiceTable(name_type, fapiao_type);
                    }
                });
                break;
            case R.id.tv_person:
                if ("2".equals(name_type)) {
                    tvPerson.setTextColor(0xd9000000);
                    tvPerson.setBackgroundResource(R.drawable.shape_line_212121);
                    tvCompany.setTextColor(0x4d000000);
                    tvCompany.setBackgroundResource(R.drawable.fapiao_selectable);
                    name_type = "1";
                    vat_type = "1";
                    changeInvoiceTable(name_type, fapiao_type);
                }
                break;
            case R.id.tv_company:
                if ("1".equals(name_type)) {
                    tvCompany.setTextColor(0xd9000000);
                    tvCompany.setBackgroundResource(R.drawable.shape_line_212121);
                    tvPerson.setTextColor(0x4d000000);
                    tvPerson.setBackgroundResource(R.drawable.fapiao_selectable);
                    name_type = "2";
                    vat_type = "2";
                    changeInvoiceTable(name_type, fapiao_type);
                }
                break;
            default:
                break;
        }
    }

    private void changeInvoiceTable(String type_name, String fapiao_type) {
        hideAll();
        if ("1".equals(type_name) && "1".equals(fapiao_type)) {//电子个人
            llElePerson.setVisibility(View.VISIBLE);
            if(mInvoiceBean!=null&&mInvoiceBean.getName_type().equals(type_name)&&mInvoiceBean.getType().equals(fapiao_type)){
                etElePersonName.setText(mInvoiceBean.getName());
                etElePersonEmail.setText(mInvoiceBean.getEmail());
            }
            tvSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = etElePersonName.getText().toString().trim();
                    String email = etElePersonEmail.getText().toString().trim();
                    InvoiceBean invoiceBean = new InvoiceBean();
                    invoiceBean.setName_type(type_name);
                    invoiceBean.setType(fapiao_type);
                    if (TextUtils.isEmpty(name)) {
                        showToast("请输入姓名");
                        return;
                    }
                    if (TextUtils.isEmpty(email)) {
                        showToast("请输入邮箱");
                        return;
                    }
                    invoiceBean.setName(name);
                    invoiceBean.setEmail(email);
                    Intent intent = new Intent();
                    intent.putExtra("invoice", invoiceBean);
                    intent.putExtra("shop_id", shop_id);
                    intent.putExtra("position", position);
//                    setIntent(intent);
                    setResult(RESULT_CODE, intent);
                    finish();
                }
            });
        } else if ("1".equals(type_name) && "2".equals(fapiao_type)) {//纸质个人
            llPaperPerson.setVisibility(View.VISIBLE);
            if(mInvoiceBean!=null&&mInvoiceBean.getName_type().equals(type_name)&&mInvoiceBean.getType().equals(fapiao_type)){
                etPaperPersonName.setText(mInvoiceBean.getName());
            }
            tvSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = etPaperPersonName.getText().toString().trim();
                    if (TextUtils.isEmpty(name)) {
                        showToast("请输入姓名");
                        return;
                    }
                    InvoiceBean invoiceBean = new InvoiceBean();
                    invoiceBean.setName_type(type_name);
                    invoiceBean.setType(fapiao_type);
                    invoiceBean.setName(name);
                    Intent intent = new Intent();
                    intent.putExtra("invoice", invoiceBean);
                    intent.putExtra("shop_id", shop_id);
                    intent.putExtra("position", position);
                    setResult(RESULT_CODE, intent);
                    finish();
                }
            });

        } else if ("2".equals(type_name) && "1".equals(fapiao_type)) {//电子公司
            llEleCompany.setVisibility(View.VISIBLE);
            if(mInvoiceBean!=null&&mInvoiceBean.getName_type().equals(type_name)&&mInvoiceBean.getType().equals(fapiao_type)){
                etEleCompanyName.setText(mInvoiceBean.getName());
                etEleCompanyEmail.setText(mInvoiceBean.getEmail());
                etEleCompanyId.setText(mInvoiceBean.getDuty_paragraph());
            }
            tvSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = etEleCompanyName.getText().toString().trim();
                    String company_id = etEleCompanyId.getText().toString().trim();
                    String email = etEleCompanyEmail.getText().toString().trim();

                    if (TextUtils.isEmpty(name)) {
                        showToast("请输入单位名称");
                        return;
                    }
                    if (TextUtils.isEmpty(email)) {
                        showToast("请输入邮箱");
                        return;
                    }


                    InvoiceBean invoiceBean = new InvoiceBean();
                    invoiceBean.setName_type(type_name);
                    invoiceBean.setType(fapiao_type);
                    invoiceBean.setName(name);
                    invoiceBean.setDuty_paragraph(company_id);
                    invoiceBean.setEmail(email);
                    Intent intent = new Intent();
                    intent.putExtra("invoice", invoiceBean);
                    intent.putExtra("shop_id", shop_id);
                    intent.putExtra("position", position);
                    setResult(RESULT_CODE, intent);
                    finish();
                }
            });

        } else if ("2".equals(type_name) && "2".equals(fapiao_type)) {//纸质公司

            if ("1".equals(invoice_type_2)) {
                vat_type = "1";
                tvPaperInvoiceType.setText("增值税专用发票");
                llSpecialInvoice.setClickable(false);
                ivZengzhiArrow.setVisibility(View.GONE);
            } else if ("2".equals(invoice_type_2)) {
                vat_type = "2";
                tvPaperInvoiceType.setText("增值税普通发票");
                llSpecialInvoice.setClickable(false);
                ivZengzhiArrow.setVisibility(View.GONE);
            }

            llSpecialInvoice.setVisibility(View.VISIBLE);
            if ("1".equals(vat_type)) {      //专用
                llPaperCompanySpecial.setVisibility(View.VISIBLE);
                if(mInvoiceBean!=null&&mInvoiceBean.getName_type().equals(type_name)&&mInvoiceBean.getType().equals(fapiao_type)&&mInvoiceBean.getVat_type().equals(vat_type)){
                    etPaperCompanyNameSpe.setText(mInvoiceBean.getName());
                    etPaperCompanyIdSpe.setText(mInvoiceBean.getDuty_paragraph());
                    etPaperCompanyAddrSpe.setText(mInvoiceBean.getAddress()+"");
                    etPaperCompanyPhoneSpe.setText(mInvoiceBean.getTel()+"");
                    etPaperCompanyBankSpe.setText(mInvoiceBean.getBank_name()+"");
                    etPaperCompanyBankidSpe.setText(mInvoiceBean.getBank_number()+"");
                }
                tvSave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = etPaperCompanyNameSpe.getText().toString().trim();
                        String company_id = etPaperCompanyIdSpe.getText().toString().trim();
                        String address = etPaperCompanyAddrSpe.getText().toString().trim();
                        String tel = etPaperCompanyPhoneSpe.getText().toString().trim();
                        String bank = etPaperCompanyBankSpe.getText().toString().trim();
                        String bankId = etPaperCompanyBankidSpe.getText().toString().trim();

                        if (TextUtils.isEmpty(name)) {
                            showToast("请输入单位名称");
                            return;
                        }
                        if (TextUtils.isEmpty(address)) {
                            showToast("请输入地址");
                            return;
                        }
                        if (TextUtils.isEmpty(company_id)) {
                            showToast("请输入纳税人识别号");
                            return;
                        }
                        if (TextUtils.isEmpty(tel)) {
                            showToast("请输入联系电话");
                            return;
                        }
                        if (TextUtils.isEmpty(bank)) {
                            showToast("请输入开户银行");
                            return;
                        }
                        if (TextUtils.isEmpty(bankId)) {
                            showToast("请输入银行账户");
                            return;
                        }
                        InvoiceBean invoiceBean = new InvoiceBean();
                        invoiceBean.setName_type(type_name);
                        invoiceBean.setType(fapiao_type);
                        invoiceBean.setVat_type(vat_type);
                        invoiceBean.setName(name);
                        invoiceBean.setDuty_paragraph(company_id);
                        invoiceBean.setAddress(address);
                        invoiceBean.setTel(tel);
                        invoiceBean.setBank_name(bank);
                        invoiceBean.setBank_number(bankId);
                        Intent intent = new Intent();
                        intent.putExtra("invoice", invoiceBean);
                        intent.putExtra("shop_id", shop_id);
                        intent.putExtra("position", position);
                        setResult(RESULT_CODE, intent);
                        finish();
                    }
                });
            } else if ("2".equals(vat_type)) {//普通
                llPaperCompany.setVisibility(View.VISIBLE);
                if(mInvoiceBean!=null&&mInvoiceBean.getName_type().equals(type_name)&&mInvoiceBean.getType().equals(fapiao_type)&&mInvoiceBean.getVat_type().equals(vat_type)){
                    etPaperCompanyName.setText(mInvoiceBean.getName());
                    etPaperCompanyId.setText(mInvoiceBean.getDuty_paragraph());
                    etPaperCompanyAddr.setText(mInvoiceBean.getAddress()+"");
                    etPaperCompanyPhone.setText(mInvoiceBean.getTel()+"");
                    etPaperCompanyBank.setText(mInvoiceBean.getBank_name()+"");
                    etPaperCompanyBankid.setText(mInvoiceBean.getBank_number()+"");
                }

                tvSave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = etPaperCompanyName.getText().toString().trim();
                        String company_id = etPaperCompanyId.getText().toString().trim();
                        String address = etPaperCompanyAddr.getText().toString().trim();
                        String tel = etPaperCompanyPhone.getText().toString().trim();
                        String bank = etPaperCompanyBank.getText().toString().trim();
                        String bankId = etPaperCompanyBankid.getText().toString().trim();
                        if (TextUtils.isEmpty(name)) {
                            showToast("请输入姓名");
                            return;
                        }
                        if (TextUtils.isEmpty(address)) {
                            showToast("请输入地址");
                            return;
                        }
                        if (TextUtils.isEmpty(company_id)) {
                            showToast("请输入纳税人识别号");
                            return;
                        }
                        InvoiceBean invoiceBean = new InvoiceBean();
                        invoiceBean.setName_type(type_name);
                        invoiceBean.setType(fapiao_type);
                        invoiceBean.setVat_type(vat_type);
                        invoiceBean.setName(name);
                        invoiceBean.setDuty_paragraph(company_id);
                        invoiceBean.setAddress(address);
                        invoiceBean.setTel(tel);
                        invoiceBean.setBank_name(bank);
                        invoiceBean.setBank_number(bankId);

                        Intent intent = new Intent();
                        intent.putExtra("invoice", invoiceBean);
                        intent.putExtra("shop_id", shop_id);
                        intent.putExtra("position", position);
                        setResult(RESULT_CODE, intent);
                        finish();
                    }
                });
            }
        }
    }

    @Override
    protected void onPause() {
        hideSoftInput(etEleCompanyEmail);
        super.onPause();
    }

    private void hideAll() {
        llElePerson.setVisibility(View.GONE);
        llEleCompany.setVisibility(View.GONE);
        llPaperCompany.setVisibility(View.GONE);
        llPaperCompanySpecial.setVisibility(View.GONE);
        llPaperPerson.setVisibility(View.GONE);
        llSpecialInvoice.setVisibility(View.GONE);
    }


}
